package com.SpringMVC.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.SpringMVC.model.TLoginMessage;
import com.SpringMVC.model.TUser;
import com.SpringMVC.service.ILoginService;

@Controller
@RequestMapping("/loginController")
public class LoginController {
	
	private ILoginService loginService;
	private TUser t;//用于取得session 中对象
	private TLoginMessage tLoginMessage;
	
	
	
	public LoginController(){}
	
	public ILoginService getLoginController() {
		return loginService;
	}
	@Autowired
	public void setLoginController(ILoginService loginService) {
		this.loginService = loginService;
	}
	
	@RequestMapping("/login")
	public String login(TUser user,HttpServletRequest request,HttpSession session){
		//System.out.println(user.gettUserName());
		String tUserPwd=user.gettUserPwd();//获取当前form中密码
		String tUserName=user.gettUserName();//获取当前form中的账号
		TUser t=loginService.loginService(tUserName);
		//将登录的用户对象 与 tUser进行绑定 放到session中
		session.setAttribute("tUser", t);
		if(t==null){
			
			
			request.setAttribute("judge", "usernamenull");
			return "login";
		}
		
		
		if(user.gettUserPwd()==null||user.gettUserPwd().length()==0){
			//返回前台数据 打印出没有输入密码
			request.setAttribute("judge", "pwdnull");
			return "login";
		}
		else if(tUserPwd.equals(t.gettUserPwd())){
			//获取登录信息将 登录信息插入到登录信息表中
			String loginIp=request.getRemoteAddr();
			System.out.println(loginIp);
			Date now =new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date=sdf.format(now).toString();
			System.out.println(date);
			tLoginMessage=new TLoginMessage();
			tLoginMessage.settLoginIp(loginIp);
			tLoginMessage.settLoginTime(date);
			tLoginMessage.settUserName(tUserName);
			tLoginMessage.settLoginId(UUID.randomUUID().toString());
			int i=loginService.addLoginMessage(tLoginMessage);
			return "admin-user";
			
		}else{
			request.setAttribute("judge", "pwdfalse");
			return "login";
			
		}
		
		
		
	}
	@RequestMapping("/changePwd")
	public String changePwd(String agoPwd,TUser tuser,HttpSession session){
		TUser t=(TUser) session.getAttribute("tUser");
		System.out.println(agoPwd);
		
		TUser tt=loginService.loginService(t.gettUserName());
		System.out.println(tt.gettUserPwd());
		
		String tUserPwd=tuser.gettUserPwd();
		System.out.println(tUserPwd);
		String tUserName=t.gettUserName();
		if(agoPwd.equals(tt.gettUserPwd())){
			
			loginService.updatePwd(tUserPwd,tUserName);
			session.setAttribute("changePwd", "pwdSuccess");
			return "admin-404";
			
		}else{
			
			session.setAttribute("changePwd", "pwdError");
			return "admin-404";
		}
		
		
	}
	
	@RequestMapping("/resetPwd")
	public String  resetPwd(TUser user,HttpSession session){
		//取得session中的对象
		TUser t=(TUser) session.getAttribute("tUser");
		//判断是不是系统管理员
		String tUserPwd=user.gettUserPwd();
		String tUserName=user.gettUserName();
		if(loginService.loginService(tUserName)==null){
			session.setAttribute("resetJudge", "userIsNull");
		}else if(t.gettUserName().equals("admin")){
			
			loginService.updatePwd(tUserPwd, tUserName);
			session.setAttribute("resetJudge", "resetSuccess");
		}else{
			session.setAttribute("resetJudge", "resetFail");
		}
		
		
		return "admin-405";
		
		
	}
	
	//获得所有账号信息
	@ResponseBody
	@RequestMapping("/getAllUserMessage")
	public Object getAllUserMessage(TUser tuser,HttpSession session){
		TUser t=(TUser) session.getAttribute("tUser");
		String sessionUserName=t.gettUserName();
		if(sessionUserName.equals("admin")){
			List<TUser> list=loginService.selectAll();
			//System.out.println(list.size());
			session.setAttribute("i", list.size());
			return list;
		}else{
			session.setAttribute("userJudege", "fail");
			return null;
		}
		
		
	}
	
	@RequestMapping("/addUser")
	public String addUser(TUser tuser,HttpSession session){
		TUser t=(TUser) session.getAttribute("tUser");
		String sessionUserName=t.gettUserName();
		if(sessionUserName.equals("admin")){
			if(tuser.gettTypeName().equals("系统管理员")){
				tuser.settId(UUID.randomUUID().toString());
				tuser.settUserType("1");				
			}else if(tuser.gettTypeName().equals("财务部主管")){
				tuser.settId(UUID.randomUUID().toString());
				tuser.settUserType("2");
				
			}else if(tuser.gettTypeName().equals("分公司财务主管")){
				tuser.settId(UUID.randomUUID().toString());
				tuser.settUserType("3");
			}
			int i=loginService.addUser(tuser);
			if(i==0){
				session.setAttribute("addUserJudge", "addFail");
			}else{
				session.setAttribute("addUserJudge", "addSuccess");
				
			}
			
			
			
		}else{
			session.setAttribute("addUserJudge", "noPower");
		}
		
		return "register";
			
	}
	@RequestMapping("/deleteUser")
	public String deleteUser(TUser tuser,HttpSession session){
		
		String tUserName=tuser.gettUserName();
		int i=loginService.deleteUser(tuser.gettUserName());
		System.out.println(i);
		return "admin-user-manage";
	}
	
	
	
	@ResponseBody
	@RequestMapping("/getLoginMessage")
	public Object getLoginMessage(HttpSession session){
		TUser t=(TUser) session.getAttribute("tUser");
		String sessionUserName=t.gettUserName();
		if(sessionUserName.equals("admin")){
			List list=loginService.selectAllLoginMessage();
			session.setAttribute("m", list.size());
			return list;
		}
		
		return null;
		
	}
	
	@ResponseBody
	@RequestMapping("/searchLoginMessage")
	public Object searchLoginMessage(String name){
		System.out.println(name);
		
		List list =loginService.search(name);
		return list;
		
	}
	
	
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		
		session.setAttribute("tUser", null);
		return "login";
		
		
	}
	
	
	@ResponseBody
	@RequestMapping("/getCompanyMessage")
	public Object getCompanyMessage(HttpSession session){
		TUser t=(TUser) session.getAttribute("tUser");
		String sessionUserName=t.gettUserName();
		if(sessionUserName.equals("admin")){
			List list=loginService.getCompanyMessage();
			session.setAttribute("c", list.size());
			return list;
		}
		
		return null;
		
	}
	

}
